package com.ehotting.edsta.circuitDesign.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.edsta.circuitDesign.controller.CircuitDesignWsController;
import com.ehotting.edsta.circuitDesign.exception.LoggerTar;
import com.ehotting.edsta.circuitDesign.service.AsyncService;
import com.ehotting.mqbd.api.TaskStateActionConstants;
import com.ehotting.mqbd.api.bean.*;
import com.ehotting.mqbd.api.bean.task.TaskStateRequest;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.service.TaskStateMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.FutureTask;


@EnableAsync
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {


    @Resource(name="mqService")
    private MQService mqService;
    @Autowired
    private CircuitDesignWsController circuitDesignWsController;
    @Autowired
    private AgentLinkManager agentLinkManager;
    @Autowired
    private TaskStateMQService taskStateMQService;


    @Async
    @Override
    public FutureTask<MQDataAble> asyncTask(MQHttpData mqData) {
        LoggerTar.mqdbReceiveData.info("mqData:{}",JSONObject.toJSONString(mqData));
        FutureTask<MQDataAble> futureTask =  mqService.requestFuture(mqData,10);
        return futureTask;
    }


    @Override
    public MQHttpData cancelTask(MQHttpData mqData) {
        LoggerTar.mqbdReceiveInfo.info("agentId:{},msg:{}",mqData.getAgentId(),mqData.getMsg());
        MQHttpData mqHttpData = (MQHttpData)mqService.request(mqData,10);
        sendWebsocket(mqData,TaskStateActionConstants.TASK_CANCEL,"任务中止");
        return mqHttpData;
    }

    @Override
    public String testCancelTask(MQHttpData mqData) {
        mqService.requestFuture(mqData,10);
        return JSON.toJSONString(mqData.getMsg());
    }

    public void sendWebsocket(MQHttpData mqData,String action,String msg){

        log.info("Start sendWebsocket.");
        TaskStateRequest response = taskStateMQService.createTaskState(
                action,
                msg,
                mqData.getAgentId());
        response.setId(String.valueOf(agentLinkManager.getProjectId(mqData.getAgentId())));
        response.setUserId(agentLinkManager.getUserId(mqData.getAgentId()));
        log.info("agentId:{},msg:{}",mqData.getAgentId(),mqData.getMsg());
        try {
            circuitDesignWsController.sendUserMessage(JSON.toJSONString(response),mqData.getAgentId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("Finish sendTaskStartWebsocket. agentId:{}",mqData.getAgentId());
    }


    public void sendWebsocket(MQHttpData mqData){
        log.info("Start sendWebsocket.");
        log.info("agentId:{},msg:{}",mqData.getAgentId(),mqData.getMsg());
        try {
            circuitDesignWsController.sendUserMessage(JSON.toJSONString(mqData.getResponse()),mqData.getAgentId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("Finish sendTaskStartWebsocket. agentId:{}",mqData.getAgentId());
    }

}